package com.zito.health.controller;

import com.zito.health.common.enums.ResultCode;
import com.zito.health.common.result.PlatformResult;
import com.zito.health.entity.ItemCertificate;
import com.zito.health.entity.ItemConclusionExpert;
import com.zito.health.service.ItemCertificateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 项目结题证书
 */
@Slf4j
@RestController
@RequestMapping("/item/Certificate")
public class ItemCertificateController {

    @Autowired
    private ItemCertificateService itemCertificateService;

    @PostMapping("/save")
    public PlatformResult save(ItemCertificate item){
        if(item.getId() == null){
            itemCertificateService.save(item);
        }else{
            itemCertificateService.update(item);
        }
        return PlatformResult.success();
    }

    @PostMapping("/query")
    public PlatformResult query(ItemCertificate item){
        return PlatformResult.success(itemCertificateService.queryOne(item));
    }

    @PostMapping("/list")
    public PlatformResult list(ItemCertificate item){
        return PlatformResult.success(itemCertificateService.query(item));
    }

    @PostMapping("/delete")
    public PlatformResult delete(Integer id){
        itemCertificateService.delete(id);
        return PlatformResult.success();
    }


    @RequestMapping("/upload")
    @ResponseBody
    public PlatformResult uploadFile(@RequestParam(value="file") MultipartFile multipartFile, HttpServletRequest request) {
        String url = request.getLocalAddr()+":"+request.getLocalPort()+request.getContextPath();
        String proPath = request.getRealPath("/");
        System.out.println(proPath);
        Calendar cal = Calendar.getInstance();
        int year = cal.get(cal.YEAR);
        int month = cal.get(cal.MONTH)+1;
        int day = cal.get(cal.DATE);
        //判断文件夹是否存在
        String path = "/upload/"+year+"/"+month+"/"+day;
        File file = new File(proPath+path);
        if(!file.exists()){//如果文件夹不存在
            file.mkdirs();//创建文件夹
        }
        String uuid = UUID.randomUUID().toString().replace("-", "");
        try {
            //获取源文件名称
            String fileName = uuid+"_"+multipartFile.getOriginalFilename();
            File file1 = new File(file,  fileName);
            multipartFile.transferTo(file1);
            Map<String,Object> res = new HashMap<>();
            res.put("path",path+"/"+fileName);
            res.put("fileName",fileName);
            return PlatformResult.success(res);
        } catch (IOException e) {
            e.printStackTrace();
            return PlatformResult.failure(ResultCode.INTERFACE_INNER_INVOKE_ERROR);
        }

    }

    @RequestMapping("/download")
    public void download(String path,HttpServletResponse response,HttpServletRequest request) {
        String proPath = request.getRealPath("/");
        File file = new File(proPath+path);
        try {
            ServletOutputStream out = response.getOutputStream();
            FileInputStream fis = new FileInputStream(file);
            byte buffer[] = new byte[1024];
            int len;
            //循环将输入流中的内容读取到缓冲区当中
            while((len=fis.read(buffer))>0) {
                //输出缓冲区的内容到浏览器，实现文件下载
                out.write(buffer, 0, len);
            }
            out.flush();
            out.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
